// pages/activity/activity.js
import {
  COS_HOST,
  httpServer
} from '../../utils/httpServer';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    COS_HOST,
    current: 1, // 1 未使用 -1 已结束
    actList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  handleQRCodeTap() {
    if (!wx.getStorageSync('openid')) {
      wx.showToast({
        icon: 'error',
        title: '请先登录',
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/qrcode/qrcode',
    })
  },

  handleSearch(e) {
    // console.log('value: ', e.detail.value);
  },

  handleStatusTap(e) {
    const current = e.currentTarget.dataset.status;
    this.setData({
      current,
    }, () => {
      this.getList();
    });
  },

  handleTabChange(e) {
    this.setData({
      current: e.detail.value,
    }, () => {
      this.getList();
    });
  },

  handleCityChange(e) {
    const index = Number(e.detail.value);
    this.setData({
      city: this.data.cityRange[index],
      pickerValue: index,
    }, () => {
      this.getList();
    });
  },

  getCityRange() {
    return new Promise((resolve, reject) => {
      httpServer('POST', 'Act', 'GetCity')
        .then(res => {
          if (res.result == 'success') {
            resolve(res);
          } else {
            reject(res);
          }
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  getList() {
    wx.showLoading({
      mask: true,
      title: '加载中',
    });
    httpServer('POST', 'User', 'GetMyActive', {
      type: this.data.current,
    }).then(res => {
      if (res.result == 'success') {
        this.setData({
          actList: res.info,
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err,
      })
    }).finally(() => {
      wx.hideLoading();
    })
  },

  handleItemTap(e) {
    const { item } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/activity-detail/activity-detail?id=' + item.id,
    });
  },

  onImageLoad(e) {
    const { index } = e.currentTarget.dataset;
    const { width, height } = e.detail;
    const realHeight = Math.round(340 * height / width);
    const { actList } = this.data;
    const target = actList[index];
    target.realHeight = realHeight;
    this.setData({
      actList,
    });
  },

  handleCityTap() {
    wx.navigateTo({
      url: '/pages/city/city',
    });
  },

  getCityName() {
    httpServer('POST', 'Act', 'GetCityName')
        .then(res => {
          if (res.result == 'success') {
              this.setData({
                city: res.info,
              }, () => {
                getApp().globalData.city = this.data.city;
              });
          }
        })
        .catch(err => {
          reject(err);
        });
  }
})